var Mock = require('mockjs')
var Random = Mock.Random
Mock.Random.extend({
    role: function(){
        const role = ['前端开发','java开发'];
        const res = this.pick(role);
        return res;
    },
    skill: function(){
        const skills = ['react','vue','html5','css','springboot'];
        const res = this.pick(skills);
        return res;
    },
    address: function(){
        const addresses = ['广州','深圳','佛山','珠海','汕头'];
        const res = this.pick(addresses);
        return res;
    }
})

let data = [];
for(let i = 0; i < 500; i++ ){
    let person = Mock.mock({
        'id':Random.id(),
        'name': Random.cname(),
        'role': '@role',
        'skill': '@skill',
        'address': '@address'
    }) 
    data.push(person);
}


// 获取用户
Mock.mock('/api/person','get',()=>{
    return{
        code: 200,
        data:data,
    }
})
// 删除用户
Mock.mock(RegExp('api/person_delete' + ".*"),'get',(options)=>{
    const personId = options.url.split('/')[3];
    if(!personId){
        return{
            code: 0,
            message: '参数错误'
        }
    }else{
        data = data.filter(item => item.id !== personId);
        return {
            code: 200,
            data: data,
            message: '删除成功'
        }
    }
})
// 查询用户
Mock.mock(RegExp('api/person_search' + ".*"),'get',(options)=>{
    let searchInfo = JSON.parse(options.url.split('/')[3]) ;
    console.log(searchInfo);

    const name = searchInfo.name;
    const role = searchInfo.role;
    const address = searchInfo.address;
    let newData;

    if(name && role && address){
        newData = data.filter(item => item.name === name && item.role === role && address.includes(item.address));
    }
    if(name && !role && address){
        newData = data.filter(item => item.name === name && address.includes(item.address));
    }
    if(name && role && !address){
        newData = data.filter(item => item.name === name && item.role === role);
    }
    if(!name && role && address){
        newData = data.filter(item => item.role === role && address.includes(item.address));
    }
    if(name && !role && address){
        newData = data.filter(item => item.name === name );
    }
    if(!name && role && !address){
        newData = data.filter(item => item.role  === role);
    }
    if(!name && !role && address){
        newData = data.filter(item => address.includes(item.address));
    }
    return {
        code: 200,
        data: newData,
        message: '查询成功'
    }
})

Mock.mock(RegExp('api/person_update' + ".*" ),'get',(options)=>{
    let updateInfo = JSON.parse(options.url.split('/')[3]);
    let personId = updateInfo.id;
    console.log(personId);
    console.log(data);
    data.some(item => {
        if(item.id === personId){
            item.name = updateInfo.name;
            item.role = updateInfo.role;
            item.skill = updateInfo.skill;
            item.address = updateInfo.address;
            return true;
        }   
    })
    console.log(data);
    return {
        code: 200,
        data: data,
        message: '查询成功'
    }
})